package org.edu.modules.kwgl.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.kwgl.entity.VeJwBkXuesheng;
import org.edu.modules.kwgl.entity.VeJwBukao;
import org.edu.modules.kwgl.entity.VeJwKssjd;
import org.edu.modules.kwgl.service.IVeJwBkJiankaoService;
import org.edu.modules.kwgl.service.IVeJwBkXueshengService;
import org.edu.modules.kwgl.service.IVeJwBukaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.kwgl.api.IVeJwJxzyFeignClientService;

import org.edu.modules.kwgl.service.IVeJwKssjdService;
import org.edu.modules.kwgl.utils.CommonAipUtils;
import org.edu.modules.scheduleCommon.dto.PageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 补考信息
 * @Author: jeecg-boot
 * @Date: 2021-05-11
 * @Version: V1.0
 */
@Api(tags = "补考信息")
@RestController
@RequestMapping("/kwgl/veJwBukao")
@Slf4j
public class VeJwBukaoController extends BaseController<VeJwBukao, IVeJwBukaoService> {
    @Autowired
    private IVeJwBukaoService veJwBukaoService;
    @Autowired
    private IVeJwBkJiankaoService veJwBkJiankaoService;
    @Autowired
    private IVeJwBkXueshengService veJwBkXueshengService;
	@Autowired
    private IVeJwJxzyFeignClientService veJwJxzyFeignClientService;
    @Autowired
    private IVeJwKssjdService veJwKssjdService;

    /**
     * 分页列表查询
     *
     * @param veJwBukao
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "补考信息-分页列表查询")
    @ApiOperation(value = "补考信息-分页列表查询", notes = "补考信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwBukao veJwBukao,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJwBukao> queryWrapper = QueryGenerator.initQueryWrapper(veJwBukao, req.getParameterMap());
//        Page<VeJwBukao> page = new Page<VeJwBukao>(pageNo, pageSize);
//        IPage<VeJwBukao> pageList = veJwBukaoService.page(page, queryWrapper);
        PageDto<VeJwBukao> page = new PageDto<>(pageNo, pageSize);
        IPage<VeJwBukao> pageList = veJwBukaoService.pageDto(page, queryWrapper);
        List<Map> list;
        for (VeJwBukao item : pageList.getRecords()) {
            list = veJwBkJiankaoService.get2teacheridByQkid(item.getId());
            item.setJiankao(list.get(0).get("teacherId").toString());
            item.setJiankaoName(list.get(0).get("xm").toString());
            item.setXunkao(list.get(1).get("teacherId").toString());
            item.setXunkaoName(list.get(1).get("xm").toString());
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param veJwBukao
     * @return
     */
    @AutoLog(value = "补考信息-添加")
    @ApiOperation(value = "补考信息-添加", notes = "补考信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwBukao veJwBukao) {
        VeJwBkXuesheng xuesheng;

        //获取课程名称
        String kcmc = veJwJxzyFeignClientService.getKcNameById(veJwBukao.getKcid());
        Result<JSONObject> result = veJwJxzyFeignClientService.getJsById(veJwBukao.getJsid());
        String jsmc = result.getResult().getString("jsmc");
        veJwBukao.setKcmc(kcmc);
        veJwBukao.setIspublish(0);

        veJwBukao.setName(kcmc + "-" + veJwBukao.getKsrq() + "-" + jsmc + "-补考");

		VeJwKssjd veJwKssjd = veJwKssjdService.getById(veJwBukao.getKssjdid());
		if(veJwKssjd!=null){
			veJwBukao.setKsrqsj(veJwKssjd.getStarttime()+"~"+veJwKssjd.getEndtime());
		}
        veJwBukaoService.save(veJwBukao);

        if (veJwBukao.getStudents() != null) {
            if (veJwBukao.getStudents().length > veJwBukao.getNumber()) {
                return Result.error("所选学生人数超过此考试人数上限！");
            }
            //插入清考学生信息//
            for (String stu : veJwBukao.getStudents()) {
                xuesheng = new VeJwBkXuesheng();
                //todo//学生信息待完善
                //xuesheng.setBkid("待设定")
                xuesheng.setId(Integer.parseInt(stu));
                xuesheng.setBkid(veJwBukao.getId());
                veJwBkXueshengService.updateById(xuesheng);
            }
        }

        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veJwBukao
     * @return
     */
    @AutoLog(value = "补考信息-编辑")
    @ApiOperation(value = "补考信息-编辑", notes = "补考信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwBukao veJwBukao) {
        //获取课程名称
        String kcmc = veJwJxzyFeignClientService.getKcNameById(veJwBukao.getKcid());
        veJwBukao.setKcmc(kcmc);
		VeJwKssjd veJwKssjd = veJwKssjdService.getById(veJwBukao.getKssjdid());
		if(veJwKssjd!=null){
			veJwBukao.setKsrqsj(veJwKssjd.getStarttime()+"~"+veJwKssjd.getEndtime());
		}
        veJwBukaoService.updateById(veJwBukao);

        if (veJwBukao.getStudents() != null) {
            if (veJwBukao.getStudents().length > veJwBukao.getNumber()) {
                return Result.error("所选学生人数超过此考试人数上限！");
            }
            //插入清考学生信息//
            for (String stu : veJwBukao.getStudents()) {
                VeJwBkXuesheng xuesheng = new VeJwBkXuesheng();
                //todo//学生信息待完善
                //xuesheng.setBkid("待设定")
                xuesheng.setId(Integer.parseInt(stu));
                xuesheng.setBkid(veJwBukao.getId());
                veJwBkXueshengService.updateById(xuesheng);
            }
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "补考信息-通过id删除")
    @ApiOperation(value = "补考信息-通过id删除", notes = "补考信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        veJwBukaoService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "补考信息-批量删除")
    @ApiOperation(value = "补考信息-批量删除", notes = "补考信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwBukaoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "补考信息-通过id查询")
    @ApiOperation(value = "补考信息-通过id查询", notes = "补考信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwBukao veJwBukao = veJwBukaoService.getById(id);
        if (veJwBukao == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwBukao);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param veJwBukao
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwBukao veJwBukao) {
        return super.exportXls(request, veJwBukao, VeJwBukao.class, "补考信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwBukao.class);
    }


    /**
     * 通过id更改发布状态
     *
     * @param id
     * @param status
     * @return
     */
    @AutoLog(value = "ve_jw_qingkao-通过id更改发布状态")
    @ApiOperation(value = "ve_jw_qingkao-通过id更改发布状态", notes = "ve_jw_qingkao-通过id更改发布状态")
    @GetMapping(value = "/changePublishById")

    public Result<?> changePublishById(@RequestParam(name = "id", required = true) int id,
                                       @RequestParam(name = "status", required = true) int status) {
        //判断是否启用
        if (status == 1) {
            if (veJwBukaoService.getById(id).getStatus() == 0) {
                return Result.error("该清考信息还未启用！");
            }
        }
        veJwBukaoService.changePublishById(id, status);
        if (status == 1) {
            return Result.ok("发布成功!");
        }
        return Result.ok("取消发布成功!");
    }

    /**
     * 批量启用/禁用
     *
     * @param ids
     * @param status
     * @return
     */
    @AutoLog(value = "教室信息表-批量启用/禁用")
    @ApiOperation(value = "教室信息表-批量启用/禁用", notes = "教室信息表-批量启用/禁用")
    @GetMapping(value = "/updateStatusBatch")
    public Result<?> updateStatusBatch(@RequestParam(name = "ids", required = true) String ids,
                                       @RequestParam(name = "status", required = true) int status) {
        veJwBukaoService.updateStatusByIds(ids, status);
        return Result.ok("更新状态成功!");
    }


    /**
     * 根据课程id查询是否存在对应补考
     *
     * @param kcid
     * @return
     */
    @AutoLog(value = "教室信息表-根据课程id查询是否存在对应补考")
    @ApiOperation(value = "教室信息表-根据课程id查询是否存在对应补考", notes = "教室信息表-根据课程id查询是否存在对应补考")
    @GetMapping(value = "/queryByKcid")
    public Result<?> queryByKcid(@RequestParam(name = "kcid", required = true) int kcid) {
        VeJwBukao bukao = veJwBukaoService.getByKcid(kcid);
        if (bukao == null) {
            return Result.error("暂无此课程的补考信息！");
        }
        return Result.ok(bukao);
    }

}
